package com.bailan.foreat.exception;

import com.bailan.foreat.entity.User;
import com.bailan.foreat.service.CartService;
import com.bailan.foreat.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class MyExceptionHandler {
    @Autowired
    private CartService cartService;
    @ExceptionHandler(value = CartException.class)
    @ResponseBody
    public String exceptionHandler(CartException e, HttpServletRequest request) {
        System.out.println("购物车异常原因=>>"+e.getMessage());
        e.printStackTrace();
        //刷新购物车
        User currentUser = UserUtils.getCurrentUser(request);
        cartService.updateCart(currentUser.getId());
        //返回异常信息
        return e.getMessage();
    }
}
